package DAO1;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

//真实实现类，实现IWorker中所有业务需求
public class WorkerDAOImpl implements IWorkerDAO {
    private Connection conn = null ;
    private PreparedStatement pstmt = null ;
    //    通过构造函数获取连接对象
    public WorkerDAOImpl(Connection conn){
        this.conn = conn ;
    }

    //    实现注册员工功能
    public boolean doCreate(Worker work) throws Exception{
        boolean flag = false ;
        String sql = "INSERT INTO worker(empno,ename,job,hiredate,sal) VALUES (?,?,?,?,?)" ;
        this.pstmt = this.conn.prepareStatement(sql) ;
        this.pstmt.setInt(1,work.getEmpno()) ;
        this.pstmt.setString(2,work.getEname()) ;
        this.pstmt.setString(3,work.getJob()) ;
        this.pstmt.setDate(4,new java.sql.Date(work.getHiredate().getTime())) ;
        this.pstmt.setFloat(5,work.getSal()) ;
        if(this.pstmt.executeUpdate() > 0){
            flag = true ;
        }
        this.pstmt.close() ;
        return flag ;
    }
    //    实现查询员工的功能
    public List<Worker> findAll(String keyWord) throws Exception{
        List<Worker> all = new ArrayList<Worker>() ;
        String sql = "SELECT empno,ename,job,hiredate,sal FROM worker WHERE ename LIKE ? OR job LIKE ?" ;
        this.pstmt = this.conn.prepareStatement(sql) ;
        this.pstmt.setString(1,"%"+keyWord+"%") ;
        this.pstmt.setString(2,"%"+keyWord+"%") ;
        ResultSet rs = this.pstmt.executeQuery() ;
        Worker work = null ;
        while(rs.next()){
            work = new Worker() ;
            work.setEmpno(rs.getInt(1)) ;
            work.setEname(((ResultSet) rs).getString(2)) ;
            work.setJob(rs.getString(3)) ;
            work.setHiredate(rs.getDate(4)) ;
            work.setSal(rs.getFloat(5)) ;
            all.add(work) ;
        }
        this.pstmt.close() ;
        return all ;
    }
    //    实现通过id查询员工的功能
    public Worker findById(int empno) throws Exception{
        Worker emp = null ;
        String sql = "SELECT empno,ename,job,hiredate,sal FROM worker WHERE empno=?" ;
        this.pstmt = this.conn.prepareStatement(sql) ;
        this.pstmt.setInt(1,empno) ;
        ResultSet rs = this.pstmt.executeQuery() ;
        if(rs.next()){
            emp = new Worker() ;
            emp.setEmpno(rs.getInt(1)) ;
            emp.setEname(rs.getString(2)) ;
            emp.setJob(rs.getString(3)) ;
            emp.setHiredate(rs.getDate(4)) ;
            emp.setSal(rs.getFloat(5)) ;
        }
        this.pstmt.close() ;
        return emp ;
    }
}
